package vista;

import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class VentanaOrden extends javax.swing.JFrame {

    private controlador.ControladorOrden control;
    private DefaultTableModel modeloTabla;
    private Vector<String> fila;
  
    public VentanaOrden() {
        initComponents();
        control= new controlador.ControladorOrden(this);
        modeloTabla = new DefaultTableModel();
        fila = new Vector<String>();
        tblProdcuto.setModel(modeloTabla);
        fila.add("Producto");
        fila.addElement("Precio");
        fila.add("Cantidad");
        fila.add("Sub-Total");
        modeloTabla.setColumnIdentifiers(fila);
        txtTotal.setEditable(false);
        addAction();
    }


     public void addListener(ActionListener escuchador){
        btnCancelar.addActionListener(escuchador);
        btnMas.addActionListener(escuchador);
        btnMenos.addActionListener(escuchador);
        btnRegistrar.addActionListener(escuchador);
        btnSalir.addActionListener(escuchador);
    }

     public void addAction(){
        btnCancelar.setActionCommand("Cancelar");
        btnMas.setActionCommand("Mas");
        btnMenos.setActionCommand("Menos");
        btnRegistrar.setActionCommand("Registrar");
        btnSalir.setActionCommand("Salir");
    }

    public void menos(){
        int i = tblProdcuto.getSelectedRow();
        if(i != -1){
            float t = Float.parseFloat(txtTotal.getText())-Float.parseFloat((String) modeloTabla.getValueAt(i, 3));
            modeloTabla.removeRow(i);
            txtTotal.setText(""+t);
        }

        else
            JOptionPane.showMessageDialog(new JFrame(),"Debe seleccionar el prodcuto que desea quitar","Cari Burguer",JOptionPane.ERROR_MESSAGE);
    }


    public LinkedList<JTextField> getTextos() {
        LinkedList<JTextField> v = new LinkedList<JTextField>();

        return v;
    }

    public boolean actualizar(){
        for (int i = 0; i < modeloTabla.getRowCount(); i++)
            if(modeloTabla.getValueAt(i, 0).equals(cmbProducto.getItemAt(cmbProducto.getSelectedIndex()))){
                int c = getCantidadProdcuto();
                double sub = Double.parseDouble(""+modeloTabla.getValueAt(i, 1))*c;
                modeloTabla.setValueAt(""+sub, i, 3);
                return true;
            }
        return false;
    }

    public void agregarFila(String[] v){
        float total;
        if(!txtTotal.getText().isEmpty())
            total = Float.parseFloat(txtTotal.getText());
        else
            total=0;
        if(!actualizar()){
            int c = getCantidadProdcuto();
            fila= new Vector<String>();
            fila.add(v[0]);
            fila.add(v[1]);
            fila.add(""+c);
            float sub = (Float.parseFloat(v[1])*c);
            fila.add("" + sub);
            total+=sub;
            txtTotal.setText(""+total);
            modeloTabla.addRow(fila);
        }
    }

    public JTextField getTxtCedula() {
        return txtCedula;
    }

    public JTextField getTxtCodigo() {
        return txtCodigo;
    }

    public JTextField getTxtTotal() {
        return txtTotal;
    }

    public LinkedList<String[]> getInfoTabla() {
        LinkedList<String []> v = new LinkedList<String[]>();
        for (int i = 0; i < modeloTabla.getRowCount(); i++){
            String [] s = new String [] {""+modeloTabla.getValueAt(i, 0),""+modeloTabla.getValueAt(i, 2)};
            v.add(s);
         }
         return v;
    }

      public boolean hayVacios(){
        for (int i = 0; i < getTextos().size(); i++) {
            JTextField texto = getTextos().get(i);
            if(texto.getText().equals("")){
                JOptionPane.showMessageDialog(new JFrame(),"Campos vacios","Cari Burguer",JOptionPane.INFORMATION_MESSAGE);
                return false;
            }
        }
        return true;
    }

    public boolean validoNumeros(){
        try{
            if(Double.parseDouble(txtCodigo.getText())<=0){
                txtCodigo.setText("");
                JOptionPane.showMessageDialog(new JFrame(),"El codigo es invalido","Cari Burguer",JOptionPane.WARNING_MESSAGE);
                return false;
           }
       }catch(NumberFormatException ex){
           JOptionPane.showMessageDialog(new JFrame(),"Ingreso letras en lugar de numeros","Cari Burguer",JOptionPane.WARNING_MESSAGE);
           return false;
       }
    return true;
    }


    public void calcularTotal(){
        float total =0;
        for (int i = 0; i < modeloTabla.getRowCount(); i++)
            total+=Float.parseFloat(""+modeloTabla.getValueAt(i, 3));
        txtTotal.setText(""+total);
    }

      public boolean validarCantidad(){
        if(getCantidadProdcuto()<=0){
                JOptionPane.showMessageDialog(new JFrame(),"Cantidad no permitida","Cari Burguer",JOptionPane.INFORMATION_MESSAGE);
                return false;
            }

        return true;
    }

     public void limpiar(){
         for (JTextField texto : getTextos()) {
            texto.setText("");
        }
        cmbProducto.setSelectedIndex(0);
        modeloTabla.setRowCount(0);
        spnCantidad.setValue(0);
        txtTotal.setText("");
     }

    public void cargarComboProductos(Vector<String> descripciones){
        cmbProducto.removeAllItems();
        for (int i = 0; i < descripciones.size(); i++) {
            cmbProducto.addItem(descripciones.elementAt(i));

        }
    }

  
    public String getTextoCmbProducto() {
        return (String) cmbProducto.getItemAt(cmbProducto.getSelectedIndex());
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        this.setTitle("Registrar Orden");
        getContentPane().setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setLayout(null);

        jPanel1.add(txtCodigo);

        jPanel1.add(txtCedula);

        {
        	jLabel4 = new javax.swing.JLabel();
        	jPanel1.add(jLabel4);
        	jLabel4.setFont(new java.awt.Font("Ubuntu",1,15));
        	jLabel4.setText("Orden");
        	jLabel4.setBounds(210, 5, 79, 22);
        }
        {
        	btnRegistrar = new javax.swing.JButton();
        	jPanel1.add(btnRegistrar);
        	btnRegistrar.setFont(new java.awt.Font("Ubuntu",1,15));
        	btnRegistrar.setText("Registrar");
        	btnRegistrar.setBounds(20, 456, 120, 23);
        	btnRegistrar.setSize(100, 23);
        }
        {
        	btnCancelar = new javax.swing.JButton();
        	jPanel1.add(btnCancelar);
        	btnCancelar.setFont(new java.awt.Font("Ubuntu",1,15));
        	btnCancelar.setText("Cancelar");
        	btnCancelar.setBounds(171, 456, 120, 23);
        	btnCancelar.setSize(100, 23);
        }
        {
        	btnSalir = new javax.swing.JButton();
        	jPanel1.add(btnSalir);
        	btnSalir.setFont(new java.awt.Font("Ubuntu",1,15));
        	btnSalir.setText("Salir");
        	btnSalir.setBounds(325, 456, 120, 23);
        	btnSalir.setSize(100, 23);
        }
        {
        	jPanel3 = new javax.swing.JPanel();
        	jPanel1.add(jPanel3);
        	jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        	jPanel3.setLayout(null);
        	jPanel3.setBounds(20, 31, 425, 77);
        	jPanel3.setBackground(new java.awt.Color(255,255,255));
        	{
        		jLabel6 = new javax.swing.JLabel();
        		jPanel3.add(jLabel6);
        		jLabel6.setFont(new java.awt.Font("Ubuntu",1,15)); // NOI18N
        		jLabel6.setText("Codigo");
        		jLabel6.setBounds(24, 7, 82, 24);
        	}
        	{
        		jLabel3 = new javax.swing.JLabel();
        		jPanel3.add(jLabel3);
        		jLabel3.setFont(new java.awt.Font("Ubuntu",1,15)); // NOI18N
        		jLabel3.setText("Cedula");
        		jLabel3.setBounds(24, 40, 82, 24);
        	}
        	{
        		txtCodigo = new javax.swing.JTextField();
        		jPanel3.add(txtCodigo);
        		txtCodigo.addActionListener(new java.awt.event.ActionListener() {
        			public void actionPerformed(java.awt.event.ActionEvent evt) {
        				txtCodigoActionPerformed(evt);
        			}
        		});
        		txtCodigo.setBounds(118, 8, 240, 22);
        		txtCodigo.setSize(240, 20);
        	}
        	{
        		txtCedula = new javax.swing.JTextField();
        		jPanel3.add(txtCedula);
        		txtCedula.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N
        		txtCedula.setBounds(118, 41, 240, 20);
        	}
        }
        {
        	jLabel11 = new javax.swing.JLabel();
        	jPanel1.add(jLabel11);
        	jLabel11.setFont(new java.awt.Font("Ubuntu",1,15));
        	jLabel11.setText("Productos");
        	jLabel11.setBounds(182, 116, 112, 22);
        }
        {
        	jPanel4 = new javax.swing.JPanel();
        	jPanel1.add(jPanel4);
        	jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        	jPanel4.setLayout(null);
        	jPanel4.setBounds(20, 140, 425, 83);
        	jPanel4.setBackground(new java.awt.Color(255,255,255));
        	{
        		jLabel9 = new javax.swing.JLabel();
        		jPanel4.add(jLabel9);
        		jLabel9.setFont(new java.awt.Font("Ubuntu",1,15));
        		jLabel9.setText("Producto");
        		jLabel9.setBounds(20, 14, 95, 21);
        	}
        	{
        		jLabel10 = new javax.swing.JLabel();
        		jPanel4.add(jLabel10);
        		jLabel10.setFont(new java.awt.Font("Ubuntu",1,15));
        		jLabel10.setText("Cantidad");
        		jLabel10.setBounds(19, 45, 95, 20);
        	}
        	{
        		spnCantidad = new javax.swing.JSpinner();
        		jPanel4.add(spnCantidad);
        		spnCantidad.setBounds(122, 44, 54, 24);
        		spnCantidad.getEditor().setPreferredSize(new java.awt.Dimension(17, 26));
        		spnCantidad.setSize(54, 20);
        	}
        	{
        		btnMas = new javax.swing.JButton();
        		jPanel4.add(btnMas);
        		btnMas.setFont(new java.awt.Font("Ubuntu",1,15));
        		btnMas.setText("Agregar");
        		btnMas.setBounds(310, 13, 102, 25);
        		btnMas.setSize(100, 23);
        	}
        	{
        		btnMenos = new javax.swing.JButton();
        		jPanel4.add(btnMenos);
        		btnMenos.setFont(new java.awt.Font("Ubuntu",1,15));
        		btnMenos.setText("Quitar");
        		btnMenos.setBounds(310, 47, 102, 23);
        		btnMenos.setSize(100, 23);
        	}
        	{
        		cmbProducto = new javax.swing.JComboBox();
        		jPanel4.add(cmbProducto);
        		cmbProducto.setBounds(121, 12, 170, 21);
        		cmbProducto.setBackground(new java.awt.Color(255,255,255));
        	}
        }
        {
        	jScrollPane1 = new javax.swing.JScrollPane();
        	jPanel1.add(jScrollPane1);
        	jScrollPane1.setBounds(20, 229, 425, 190);
        	jScrollPane1.setBackground(new java.awt.Color(255,255,255));
        	{
        		tblProdcuto = new javax.swing.JTable();
        		jScrollPane1.setViewportView(tblProdcuto);
        	}
        }
        {
        	jLabel5 = new javax.swing.JLabel();
        	jPanel1.add(jLabel5);
        	jLabel5.setFont(new java.awt.Font("Ubuntu",1,15)); // NOI18N
        	jLabel5.setText("Total");
        	jLabel5.setBounds(265, 424, 55, 30);
        	jLabel5.setSize(55, 23);
        }
        {
        	txtTotal = new javax.swing.JTextField();
        	jPanel1.add(txtTotal);
        	txtTotal.setFont(new java.awt.Font("Arial", 0, 13));
        	txtTotal.setBounds(325, 424, 120, 23);
        	txtTotal.setSize(120, 20);
        }
        
        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 0, 466, 499);
        jPanel1.setBackground(new java.awt.Color(255,255,255));

        tblProdcuto.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        this.setBounds(0, 0, 472, 524);
    }// </editor-fold>//GEN-END:initComponents

    private void txtCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCodigoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtCodigoActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaOrden().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnMas;
    private javax.swing.JButton btnMenos;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox cmbProducto;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner spnCantidad;
    private javax.swing.JTable tblProdcuto;
    private javax.swing.JTextField txtCedula;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtTotal;
    // End of variables declaration//GEN-END:variables

    public String [] getRegistro() {
          String [] s = new String [] {""+cmbProducto.getItemAt(cmbProducto.getSelectedIndex()),""+getCantidadProdcuto()};

         return s;
    }

    public Integer getCantidadProdcuto() {
        return (Integer) spnCantidad.getValue();
    }



}
